import Router from '../../utils/router';
import base from '../../config/base.js';
import service from '../../services/collage';
import Record from '../../utils/page-record.js';
import ext from '../../utils/login';
import free from '../../services/free.js'
var scrollTime = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_mask_sellout: base.static + 'collage/out.png',
    store_id: '',
    user_id: '',
    helpFriend: false, //助力弹窗
    goodsList: '',
    endBloon: true,
    loadBloon: true,
    page: '',
    scrolltop: '',
    nomore: true,
    shareInfo: '',
    statusCode: '', //助力状态
    ceorid: '', //订单id
    noauthorize: false, //授权状态
    user_info: '',
    share_url: '', //分享图片
    need_people: '',
    todayCollageTimes: '', //今日助力次数
    success: false, //自己可以领取
    order_status: false,
    isShenhe: 0,
    mask_box: false,
    transmit: {
      style: 'width: 325rpx; height: 100rpx;font-size: 34rpx;color: #66CC66;text-align: center;line-height: 100rpx;',
      value: '去抽奖'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let user_info = wx.getStorageSync('user_info')
    that.setData({
      store_id: options.store_id,
      ceorid: options.ceorid,
      user_info: user_info,
    })
    if (user_info.info_authorize !=1){
      that.setData({
        noauthorize: true
      })
    }
    that.shareInto(options.ceorid);
    that.getFreeList(1);
    wx.hideShareMenu();
  },
  // 更改拼团id
  changeId: function (e) {
    let that = this;
    let param = {};
    let inx = e.detail.inx;
    let strings = "goodsList[" + inx + "].ceorid"
    param[strings] = e.detail.ceorid;
    that.setData(param);
  },
  // 分享进入
  shareInto: function (ceorid) {
    let that = this;
    let params = {
      ceorid: ceorid
    }
    service.collagefriendcheck(params, function (res) {
      if(res.code && res.code == 200){
        that.setData({
          shareInfo: res.data,
          todayCollageTimes: parseInt(res.data.todaySumTimes) - parseInt(res.data.todayCollageTimes),
          helpFriend: true,
        })
      } else if (res.code && res.code == 4001) { //免费拿列表该商品已不在进行
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4002) { //商品库存为0
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4003) { //商品已下架
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4004) { //免费拿订单已助力成功
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4005) { //免费拿订单已助力失败
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4006) { //该用户当天助力次数已达上限
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4007) { //该用户为该好友助力次数已达上限
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4008) { //已经帮该好友当前订单助力过
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
        })
      } else if (res.code && res.code == 4009) { //自己不能帮自己助力
        that.setData({
          shareInfo: res.data,
          statusCode: res.code,
          need_people: parseInt(res.data.collageOrder.need_people) - parseInt(res.data.collageOrder.have_people),
        });
        if (res.data.collageOrder.order_status == 2){
          that.setData({
            success: true,
          })
        }
        that.goodsDetil();
      }
    });
  },
  //商品详情
  goodsDetil: function(){
    let that = this;
    let params = {
      ceorid: that.data.ceorid
    }
    free.getdetail(params, function (res) {
      if (res.code && res.code == 1) {
        that.setData({
          share_url: 'https:' + res.data.orders.share_url,
        })
      }
    });
  }, 
  // 买家给好友助力
  helpFriend: function(){
    let that = this;
    let params = {
      ceorid: that.data.ceorid
    }
    service.collagefriendadd(params, function (res) {
      if (res.code && res.code == 201) {
        that.setData({
          statusCode: res.code,
          helpFriend: false,
          mask_box:true
        })
      }
    });
  },
  // 去抽奖
  goluck() {
    let store_id = wx.getStorageSync('user_info').lastView.seid || '';    
    this.setData({
      mask_box: false
    })
    Router.go('luck', `store_id=${store_id}`)
  },
  // 一会再抽
  cancleluck() {
    this.setData({
      mask_box: false
    })
  },
  //特惠列表
  getFreeList: function(page){
    let that = this;
    let params = {}
    service.getcheap_goods(params, function (res) {
      if (res.code && res.code == 200) {
        let stolist = res.data
        for (let i = 0; i < res.data.length; i++){
          stolist[i].goods_pic = [{url: ''}]
          stolist[i].goods_pic[0].url = res.data[i].pic
        }
        that.setData({
          goodsList: stolist,
          nomore: false,
        })
      }
    });
    wx.stopPullDownRefresh()
  },
  // 去免费拿
  goFree: function(){
    let that = this;
    that.setData({
      success: false
    })
    Router.go('freecharege', `store_id=${that.data.store_id}`)
  },
  // 去提交订单
  goSubaddress: function(){
    let that = this;
    that.setData({
      success: false
    })
    Router.go('subaddress', `ceorid=${that.data.ceorid}`)
  },
  // 去规则
  goWeb: function () {
    let url = encodeURIComponent('https://kpxd.zhengdianhaohuo.com/h5/#/active/win3_790')
    Router.go('web', `url=${url}`)
  },
  //监听页面高度
  scrollTimer: null,
  onPageScroll: function (e) {
    let that = this;
    if (that.scrollTimer) {
      clearTimeout(that.scrollTimer)
    };
    that.scrollTimer = setTimeout(() => {
      that.setData({
        scrolltop: e.scrollTop
      })
    }, 500);
  },
  //返回顶部
  goscoltop: function () {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  //授权
  onGetUserInfo: function (e) {
    let that = this;
    let d = e.detail;
    if (d.errMsg == 'getUserInfo:ok') {
      ext.getUserInfo(d, b => {
        if (b) {
          that.setData({
            noauthorize: false,
          })
        }
      });
    }
  },
  // 记录继续邀请
  btnClick: function(){
    let p = getCurrentPages().pop();
    Record.clickButton(p, 'Invitation_free')
  },
  //红包记录
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    that.getFreeList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let that = this;
    let title = wx.getStorageSync('user_info').user_nickname + '@你，快来帮点一下，你也可以免费拿！';
    let user_id = wx.getStorageSync('user_info').id
    let paths = 'pages/store/index?store_id=' + that.data.store_id + '&to=' + encodeURIComponent('/pages/collage/helpfree?ceorid=' + that.data.ceorid + '&sharerecord=freehelpcard' + '&store_id=' + that.data.store_id + '&user_id=' + user_id);
    return {
      title: title,
      path: paths,
      imageUrl: that.data.share_url
    }
  }
})